package cn.dakaqi.utils.rongYun;

import cn.dakaqi.utils.BaseDAO;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.rongYun.models.SdkHttpResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团小助手
 * Created by chunyang on 2016/6/5.
 */
public class PushGroupMesssage
{
    public static final String TYPE_ACTIVITY_DETAIL_PAGE = "ACTIVITY_DETAIL_PAGE";  //活动详情页面
    public static final String GROUP_VOLUNTEER_WAITE_PAGE = "GROUP_VOLUNTEER_WAITE_PAGE";  //社团成员待审核页面
    public static final String GROUP_RECRITE_VOLUNTEER_WAITE_PAGE = "RECRITE_VOLUNTEER_WAITE_PAGE";  //社团招募成员待审核页面
    //社团小助手
    public static final String GROUP_HELPER = "63fd4c29-75d4-4571-98ef-e658ec685048";
    //消息群组分类
    public static final String GROUP_TYPE_GROUP= "G";
    public static final String GROUP_TYPE_ACTIVITY = "A";
    @Autowired
    BaseDAO baseDAO;

    static PushGroupMesssage pushGroupMesssage = null;

    private PushGroupMesssage()
    {
    }



    /**
     * 类级的内部类，也就是静态的成员式内部类，该内部类的实例与外部类的实例
     * 没有绑定关系，而且只有被调用到时才会装载，从而实现了延迟加载。
     */
    private static class SingletonHolder
    {
        /**
         * 静态初始化器，由JVM来保证线程安全
         */
        private static PushGroupMesssage instance = new PushGroupMesssage();
    }

    public static PushGroupMesssage getInstance()
    {
        return SingletonHolder.instance;
    }


    /**
     * 报名参加一个社团
     */
    public void applyGroupVolunteer(String name,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        PushMessageUtil.pushMessage("您提交了"+name+"的申请，正在审核中，请耐心等待。", null, GROUP_HELPER, volunteers);
    }

    /**
     * 报名参加一个社团招募
     */
    public void applyGroupRecruit(String name,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        PushMessageUtil.pushMessage("您提交了"+name+"的申请，正在审核中，请耐心等待。", null, GROUP_HELPER, volunteers);
    }

    /**
     *审核社团招募成员加入
     * @param groupName
     * @param volunteerName
     * @param groupRecruitVolunteerId
     */
    public void waiteVerfierGroupReuitelunteer(String groupName, String volunteerName,String volunteerMemberCode,String adminMemberCode,long groupRecruitVolunteerId)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(adminMemberCode);
        Map<String, String> params = new HashMap<String, String>();
        params.put("groupRecruitVolunteerId",groupRecruitVolunteerId+"");
        params.put("otherMemberCode",volunteerMemberCode);
        params.put("type",GROUP_RECRITE_VOLUNTEER_WAITE_PAGE);
        PushMessageUtil.pushMessage(volunteerName+"申请加入"+groupName+"招募，点击审核。",params, GROUP_HELPER, volunteers);
    }


    /**
     *审核社团成员加入
     * @param baseDAO
     * @param groupId
     * @param groupName
     * @param volunteerName
     * @param volunteerMemberCode
     * @param groupVolunteerId
     */
    public void waiteVerfierGroupVolunteer(BaseDAO baseDAO,long groupId,String groupName, String volunteerName,String volunteerMemberCode,long groupVolunteerId)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        //查询活动的所有报名记录
        String sql = "SELECT MEMBER_CODE FROM U_VOLUNTEER AS A INNER JOIN (SELECT VOLUNTEER_ID FROM U_GROUP_VOLUNTEER WHERE GROUP_ID = " + groupId + " AND ROLE > 0 AND STATUS = "+DKQConstant.APPLY_STATUS_OK+" AND DEL_STATUS = "  + DKQConstant.DEL_NO + ") AS B ON B.VOLUNTEER_ID = A.ID";
        List<Map<String,String>> list = baseDAO.queryNativeSql(sql);
        if(null != list && list.size()>0)
        {
            for(Map<String,String> map:list)
                volunteers.add(map.get("MEMBER_CODE"));
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("groupVolunteerId",groupVolunteerId+"");
        params.put("otherMemberCode",volunteerMemberCode);
        params.put("type",GROUP_VOLUNTEER_WAITE_PAGE);
        PushMessageUtil.pushMessage(volunteerName+"申请加入"+groupName+"，点击审核。",params, GROUP_HELPER, volunteers);
    }

    /**
     * 管理员修改活动
     * @param activityCode
     * @param activityName
     * @param adminName
     * @param activityId
     */
    public void updateActivity(BaseDAO baseDAO,String activityCode,String activityName,String adminName,long activityId)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        //查询活动的所有报名记录
        String sql = "SELECT MEMBER_CODE FROM U_VOLUNTEER AS A INNER JOIN (SELECT VOLUNTEER_ID FROM U_ACTIVITY_APPLY WHERE ACTIVITY_ID = " + activityId + " AND STATUS = "+DKQConstant.APPLY_STATUS_OK+" AND DEL_STATUS = "  + DKQConstant.DEL_NO + ") AS B ON B.VOLUNTEER_ID = A.ID";
        List<Map<String,String>> list = baseDAO.queryNativeSql(sql);
        if(null != list && list.size()>0)
        {
            for(Map<String,String> map:list)
                volunteers.add(map.get("MEMBER_CODE"));
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("activityCode",activityCode);
        params.put("type",TYPE_ACTIVITY_DETAIL_PAGE);
        PushMessageUtil.pushMessage("管理员"+adminName+"修改了"+activityName+"，点击查看详情。", params, GROUP_HELPER, volunteers);
    }
    /**
     * 取消社团管理员
     * @param groupName
     * @param adminName
     * @param memberCode
     */
    public void cancelGroupAdmin(String groupName, String adminName,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        PushMessageUtil.pushMessage(groupName+adminName+"取消您的管理员权限。。",null, GROUP_HELPER, volunteers);
    }

    /**
     * 设置社团管理员
     * @param groupName
     * @param memberCode
     */
    public void setGroupAdmin(String groupName,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        PushMessageUtil.pushMessage(groupName+"设置您为管理员。",null, GROUP_HELPER, volunteers);
    }
    /**
     * 社团发布了新的活动
     * @param activityCode
     * @param activityName
     * @param groupId
     * @param groupName
     */
    public void createNewActivity(BaseDAO baseDAO,String activityCode,String activityName,long groupId,String groupName)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        //查询活动的所有报名记录
        String sql = "SELECT MEMBER_CODE FROM U_VOLUNTEER AS A INNER JOIN (SELECT VOLUNTEER_ID FROM U_GROUP_VOLUNTEER WHERE GROUP_ID = " + groupId + " AND STATUS = "+ DKQConstant.APPLY_STATUS_OK+" AND DEL_STATUS = "  + DKQConstant.DEL_NO + ") AS B ON B.VOLUNTEER_ID = A.ID";
        List<Map<String,String>> list = baseDAO.queryNativeSql(sql);
        if(null != list && list.size()>0)
        {
            for(Map<String,String> map:list)
                volunteers.add(map.get("MEMBER_CODE"));
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("activityCode",activityCode);
        params.put("type",TYPE_ACTIVITY_DETAIL_PAGE);
        PushMessageUtil.pushMessage(groupName+"发布了"+activityName+"活动，点击报名。", params, GROUP_HELPER, volunteers);
    }

    /**
     * 同意申请人参加社团招募
     * @param name
     * @param memberCode
     */
    public void agreeGroupRecruit(String name,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        Map<String, String> params = new HashMap<String, String>();

        PushMessageUtil.pushMessage(name + "社团招募同意了您的申请，您已经是该社团成员。", params, GROUP_HELPER, volunteers);
    }

    /**
     * 同意用户加入本社团
     * @param groupName
     * @param memberCode
     */
    public void agreeGroupVolunteer(String groupName,String memberCode)
    {
        //推送消息至志愿者用户
        List<String> volunteers = new ArrayList<String>();
        volunteers.add(memberCode);
        Map<String, String> params = new HashMap<String, String>();

        PushMessageUtil.pushMessage(groupName + "同意您的申请，您已经是该组织成员。", params, GROUP_HELPER, volunteers);
    }

    /**
     * 创建群组
     * @param userIds
     * @param groupCode
     * @param groupName
     */
    public void createGroup(String type,List<String> userIds,String groupCode,String groupName)
    {
        groupCode =type+"|"+groupCode;
        SdkHttpResult result = PushMessageUtil.createGroup(userIds,groupCode,groupName);
        if(result.getHttpCode() == 200)
        {
            List<String> toGroupIds = new ArrayList<String>();
            toGroupIds.add(groupCode);
            if(type.equals("G"))
                result = PushMessageUtil.publishGroupMessage(GROUP_HELPER,toGroupIds,"系统提示您在聊天中，请注意财产安全",groupName);
            else
                result = PushMessageUtil.publishGroupMessage(PushActivityMesssage.ACTIVITY_HELPER,toGroupIds,"系统提示您在聊天中，请注意财产安全",groupName);
        }
    }

    public static void main(String[] args)
    {
//        List<String> userIds = new ArrayList<String>();
//        userIds.add("402882f655591a000155591bf46f0001");
//        String groupCode = "1120160000055";//UUIDHexGenerator.generator()
//        String groupName = "测试组";//UUIDHexGenerator.generator();
//        groupCode ="A|"+groupCode;
//        SdkHttpResult result = PushMessageUtil.createGroup(userIds,groupCode,groupName);
//        if(result.getHttpCode() == 200)
//        {
//            System.out.println(result+"----------1--------->"+userIds.get(0)+ "    " + groupCode + "   "+groupName);
//            List<String> toGroupIds = new ArrayList<String>();
//            toGroupIds.add(groupCode);
//            result = PushMessageUtil.publishGroupMessage(GROUP_HELPER,toGroupIds,"请在聊天中注意人身财产安全",groupName);
//            System.out.println(result+"----------2--------->"+GROUP_HELPER+ "   " + groupCode + "    "+groupName);
//        }


        String groupCode ="A|3120160000070";
        String userId = "402882f6555c0ead01555c0ead7d0000";
        String groupName = "6.27 247，活动群聊";
        SdkHttpResult result = PushMessageUtil.joinGroup(userId, groupCode, groupName);
        if(result.getHttpCode() == 200)
        {
            List<String> toGroupIds = new ArrayList<String>();
            toGroupIds.add(groupCode);
            PushMessageUtil.publishGroupMessage(userId,toGroupIds,"大家好",groupName);
        }


    }
    /**
     * 加入群组
     * @param userId
     * @param groupCode
     * @param groupName
     */
    public void joinGroup(String type,String userId,String userName,String groupCode,String groupName)
    {

        groupCode =type+"|"+groupCode;
        SdkHttpResult result = PushMessageUtil.joinGroup(userId, groupCode, groupName);
        System.out.println(result);
        if(result.getHttpCode() == 200)
        {
            System.out.println(userName+"成功加入了"+groupName + "群组");
            List<String> toGroupIds = new ArrayList<String>();
            toGroupIds.add(groupCode);
            if(type.equals("A"))
                userId = PushActivityMesssage.ACTIVITY_HELPER;
            else
                userId = PushGroupMesssage.GROUP_HELPER;

            result = PushMessageUtil.publishGroupMessage(userId,toGroupIds,"欢迎"+userName+"加入本群组.",groupName);
        }
    }

    /**
     * 退出群组
     * @param userId
     * @param groupCode
     */
    public void quitGroup(String userId,String groupCode)
    {
        PushMessageUtil.exitGroup(userId, groupCode);
    }
}
